package com.vtquang.mymaps.adapter;


import com.vtquang.mymaps.MyAddress;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAddressAdapter {
	private DatabaseHelper mDBHelper;
	private final Context mmContext;
	private SQLiteDatabase mDB;

	private static final int MAX_RECORD = 5;
	private static final String DATABASE_NAME = "MY_MAPS";
	private static final String DATABASE_TABLE = "MY_ADRESS_SEARCH";
	private static final int DATABASE_VERSION = 1;

	static final String colID = "_id";
	static final String colName = "_addressName";
	static final String colLocal = "_local";
	static final String colLng = "_lng";
	static final String colLat = "_lat";

	private static final String DATABASE_CREATE = "CREATE TABLE "
			+ DATABASE_TABLE + "( " + colID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + colName + " text, "
			+ colLocal + " text, " + colLng + " text, " + colLat + " text)";

	public DBAddressAdapter(Context c) {
		mmContext = c;
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {
		public DatabaseHelper(Context context, String name,

		CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS users");
			onCreate(db);
		}
	}

	public DBAddressAdapter open() {
		mDBHelper = new DatabaseHelper(mmContext, DATABASE_NAME, null,
				DATABASE_VERSION);
		mDB = mDBHelper.getWritableDatabase();
		return this;
	}

	public void delete_byID(String id) {
		mDB.delete(DATABASE_TABLE, colID + "=" + id, null);
	}

	public long InsertAddress(MyAddress myaddress) {
		Cursor mCursor = getAllAddress();

		if (mCursor != null) {
			int count = mCursor.getCount();
			mCursor.moveToFirst();
			for (int i = 0; i < count; i++) {
				double lng = mCursor.getDouble(3);
				double lat = mCursor.getDouble(4);
				
				if(lng == myaddress.get_lon() && lat == myaddress.get_lat()){
					delete_byID(mCursor.getString(0));
					count = count - 1;
					break;
				}
				
				if(!mCursor.moveToNext())
					break;
			}
			
			if (count >= MAX_RECORD) {
				mCursor.moveToFirst();
				String id = mCursor.getString(0);
				delete_byID(id);
			}
		}
		
		ContentValues inititalValues = new ContentValues();
		inititalValues.put(colName, myaddress.get_name());
		inititalValues.put(colLocal, myaddress.get_location());
		inititalValues.put(colLng, myaddress.get_lon());
		inititalValues.put(colLat, myaddress.get_lat());
		return mDB.insert(DATABASE_TABLE, null, inititalValues);
	}

	public Cursor getAllAddress() {
		return mDB.query(DATABASE_TABLE, new String[] { colID, colName,
				colLocal, colLng, colLat }, null, null, null, null, null);
	}

	public void update_byID(MyAddress myaddress) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(colName, myaddress.get_name());
		contentValues.put(colLocal, myaddress.get_location());
		contentValues.put(colLng, myaddress.get_lon());
		contentValues.put(colLat, myaddress.get_lat());
		mDB.update(DATABASE_TABLE, contentValues,
				colLng + "=" + myaddress.get_lon() + " and " + colLat + "="
						+ myaddress.get_lat(), null);
	}

	public int deleter() {
		int count = 0;
		try {
			count = mDB.delete(DATABASE_TABLE, null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return count;
	}

	public void close() {
		mDBHelper.close();
		mDB.close();
	}
}
